package cn.yunhe.service.impl;

import cn.yunhe.dao.ExamPaperDao;
import cn.yunhe.dao.QuestionDao;
import cn.yunhe.entity.ExamPaper;
import cn.yunhe.entity.Question;
import cn.yunhe.service.ExamPaperService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.annotations.Insert;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service("examPaperService")
public class ExamPaperServiceImpl implements ExamPaperService {

    @Resource
    private ExamPaperDao examPaperDao;

    @Resource
    private QuestionDao questionDao;

    /**
     * 根据标题,分类,分页模糊查询现有试卷
     * @param cond
     * @return
     * author guohanyu
     */
    @Override
    public Page getPapersByFieldAndPointAndName(Map<String, Object> cond) {
        //先创建一个page对象
        Page page = new Page();
        //得到记录总数.付给page的total属性
        page.setTotal(examPaperDao.getPapersByFieldAndPointAndNameCount(cond));
        //获取每页大小
        int pageSize = Integer.parseInt(String.valueOf(cond.get("pageSize"))) ;
        page.setPageSize(pageSize);
        //获取当前页码
        int currPageNum =Integer.parseInt(String.valueOf(cond.get("pageNum"))) ;
        //计算出总页数
        page.setPageNum((int)(page.getTotal()/pageSize+(page.getTotal()%pageSize==0? 0:1)));
        //启用分页
        page = PageHelper.startPage(currPageNum,pageSize);
        //查询列表
        List list = examPaperDao.getPapersByFieldAndPointAndName(cond);

        return page;
    }


    /**
     * 发布考试,触发试题查询,插入等操作,需添加事务
     * @return
     * Map中包含数据(examPaper相关信息,questionCount,pointId集合)
     * 郭瀚钰
     */
    @Transactional(value="txManager1")
    @Override
    public boolean addExamPaper(Map map) throws RuntimeException {
        //定义一个标示
        boolean flag = true;
            //首先执行像试卷表插入试卷信息
            ExamPaper examPaper = (ExamPaper) map.get("examPaper");
            if (!examPaperDao.addExamPaper(examPaper)){
               return  flag = false;
            }
            //根据知识点从题库中获取试题
            System.out.println(map.get("pointList"));
            System.out.println(map.get("questionCount"));
            List<Question> questionList = questionDao.getSeveralQuestion(map);
            //向试卷试题表插入试题
            for (Question ql : questionList) {
                map.put("questionId", ql.getId());
                map.put("examPaperId", examPaper.getId());
                if (!questionDao.addQuestionsToExamPaper(map)){
                   return   flag = false;
                }
            }

        return flag;
    }


    public QuestionDao getQuestionDao() {
        return questionDao;
    }

    public void setQuestionDao(QuestionDao questionDao) {
        this.questionDao = questionDao;
    }

    public ExamPaperDao getExamPaperDao() {
        return examPaperDao;
    }

    public void setExamPaperDao(ExamPaperDao examPaperDao) {
        this.examPaperDao = examPaperDao;
    }
}
